package com.yyc.system.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yyc.core.Result;
import com.yyc.core.ResultGenerator;
import com.yyc.system.model.TTeacher;
import com.yyc.system.model.TUser;
import com.yyc.system.service.ITTeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyc.system.service.ITUserService;
import com.yyc.utils.CommonUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
* Created by yyc on 2019/01/01.
*/
@RestController
@RequestMapping("/t/teacher")
public class TTeacherController {
    @Autowired
    private ITTeacherService tTeacherService;
    @Autowired
    private ITUserService tUserService;

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map) {
        TUser user = JSON.parseObject(JSON.toJSONString(map.get("user")), TUser.class);
        tUserService.save(user);
        String collegeId = CommonUtils.getMapValue(map, "collegeId");
        String teacherPosition = CommonUtils.getMapValue(map, "teacherPosition");
        TTeacher tTeacher = new TTeacher();
        tTeacher.setCollegeId(Integer.valueOf(collegeId));
        tTeacher.setTeacherPosition(teacherPosition);
        tTeacher.setUserId(user.getId());
        tTeacherService.save(tTeacher);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam String ids) {
        tTeacherService.deleteByIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TTeacher tTeacher) {
        tTeacherService.update(tTeacher);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        TTeacher tTeacher = tTeacherService.findById(id);
        return ResultGenerator.genSuccessResult(tTeacher);
    }

    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TTeacher> list = tTeacherService.findTeacherList();
        PageInfo<TTeacher> pageInfo = new PageInfo<TTeacher>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    
    //*****************************************************************************

    @RequestMapping("/searchTeacher")
    public Result searchTeacher() {
        List<TTeacher> teacherList = tTeacherService.searchTeacher();
        return ResultGenerator.genSuccessResult(teacherList);
    }
}
